//
//  LoginViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/3/23.
//

#import "LoginViewController.h"
#import "MZ_AppleLoginManager.h"
#import <YYText.h>
#import "NumberViewController.h"
#import <ATAuthSDK/ATAuthSDK.h>
#import <YTXMonitor/YTXMonitor.h>
#import <YTXOperators/YTXOperators.h>
@interface LoginViewController ()
@property (nonatomic , assign) BOOL isXieyi;
@property (nonatomic , strong) SQCustomButton * right2;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = SQColor(@"#000000");
    [self SQ_Header];
    [[SQWebSocket shareManager] SQ_close:^(NSInteger code, NSString * _Nonnull reason, BOOL wasClean) {
        DTLog(@"%ld",(long)code);
     }];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"userinfo_switch" forKey:@"key"];
    [HttpTool post:Url52 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSString * value = [NSString stringWithFormat:@"%@",[data objectForKey:@"value"]];
        [Manager SQ_SetObject:value FormKey:@"userinfo_switch"];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}

- (void)printFontName
{
    for(NSString *fontFamilyName in [UIFont familyNames]){
        NSLog(@"family:'%@'",fontFamilyName);
        for(NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]){
            NSLog(@"\tfont:'%@'",fontName);
        }
        NSLog(@"-------------");
    }
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bg = [[UIImageView alloc]init];
    bg.image = SQGetImage(@"logoTop");
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(240);
        make.width.mas_equalTo(SCREEN_WIDETH-140);
        make.height.mas_equalTo(135);
    }];
 
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *applocalversion = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    
    UILabel * label = [[UILabel alloc]init];
    label.textColor = UIColor.whiteColor;
    label.font = FONT_SEMIBOLD(28);
    label.text = [NSString stringWithFormat:@"%@",applocalversion];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(bg.mas_bottom).offset(20);
    }];
    
    SQCustomButton * right = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(0, 0) midmargin:0];
    right.SQBackimageView.image = SQGetImage(@"按钮背景");
    right.text = SQNSLocalString(@"本机号码一键登录");
    right.textColor = UIColor.whiteColor;
    right.font = FONT_SEMIBOLD(17);
    right.layer.cornerRadius = 27;
    [self.view addSubview:right];
   
    [right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-220);
        make.height.mas_equalTo(54);
        make.width.mas_equalTo(SCREEN_WIDETH-60);
        make.centerX.equalTo(self.view);
    }];
    WS(weakSelf);
    [right setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_Header];
        [weakSelf SQ_pgLogin];
    }];
    
    self.right2 = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(0, 0) midmargin:0];
    self.right2.text = SQNSLocalString(@"验证码登录");
    self.right2.textColor = UIColor.blackColor;
    self.right2.font = FONT_SEMIBOLD(17);
    self.right2.backgroundColor = UIColor.whiteColor;
    self.right2.layer.cornerRadius = 27;
    [self.view addSubview:self.right2];
   
    [self.right2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-150);
        make.height.mas_equalTo(54);
        make.width.mas_equalTo(SCREEN_WIDETH-60);
        make.centerX.equalTo(self.view);
    }];
 
    [self.right2 setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_Header];
        [weakSelf SQ_number];
    }];
    
    YYLabel * label3 = [[YYLabel alloc]initWithFrame:CGRectMake(SCREEN_WIDETH/2.f-100, SCREEN_HEIGHT- FootHeight-40, 260, 40)];
    label3.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    label3.textAlignment = NSTextAlignmentCenter;
    label3.numberOfLines = 0;
    [self.view addSubview:label3];
    NSDictionary *attributes = @{NSFontAttributeName:FONT_MEDIUM(12) , NSForegroundColorAttributeName: SQColor(@"#DBDBDB")};
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:SQNSLocalString(@"我已阅读并同意 《用户协议》、《隐私协议》") attributes:attributes];
   
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"《用户协议》")] color:SQColor(@"#FFFFFF") backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        CommonWebVcView *vc= [[CommonWebVcView alloc]init];
        vc.urlStr =  YHXY;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    NSRange range1=[[text string]rangeOfString:SQNSLocalString(@"《用户协议》")];
    YYTextDecoration* deco=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:0] color:[UIColor whiteColor]];
    [text yy_setTextUnderline:deco range:range1];
    
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"《隐私协议》")] color:SQColor(@"#FFFFFF") backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        CommonWebVcView *vc= [[CommonWebVcView alloc]init];
        vc.urlStr =  YSTK;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    NSRange range2=[[text string]rangeOfString:SQNSLocalString(@"《隐私协议》") ];
    YYTextDecoration* deco2=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:0] color:[UIColor whiteColor]];
    [text yy_setTextUnderline:deco2 range:range2];
    label3.attributedText = text;
    
    SQCustomButton * leftbtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(15, 15) midmargin:6];
    leftbtn.image = SQGetImage(@"选择");
    [self.view addSubview:leftbtn];
    [leftbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label3);
        make.height.mas_equalTo(40);
        make.right.equalTo(label3.mas_left);
        make.width.mas_equalTo(40);
    }];
    
    [leftbtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        weakSelf.isXieyi = !weakSelf.isXieyi;
        if (weakSelf.isXieyi) {
            button.image = SQGetImage(@"未选择");
        } else {
            button.image = SQGetImage(@"选择");
        }
    }];
}
-(void)SQ_number{
    if (self.isXieyi) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请查看并勾选协议")];
        return;
    }
    NumberViewController * vc = [[NumberViewController alloc]init];
    [self SQ_pushViewControllerFull:vc];
}

-(void)SQ_pgLogin{
    if (self.isXieyi) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请查看并勾选协议")];
        return;
    }
    WS(weakSelf);
    [[TXCommonHandler sharedInstance] getLoginTokenWithTimeout:10 controller:self model:[self SQ_getModel] complete:^(NSDictionary * _Nonnull resultDic) {
        NSString * resultCode = [NSString stringWithFormat:@"%@",[resultDic objectForKey:@"resultCode"]];
        if ([resultCode isEqualToString:@"600000"]) {
            [weakSelf SQ_login:[resultDic objectForKey:@"token"]];
        }else if ([resultCode intValue] > 600002 && [resultCode intValue] <700000){
            [YJProgressHUD showMsgWithoutView:[resultDic objectForKey:@"msg"]];
        }
    }];
}

-(void)SQ_login:(NSString*)code{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:code forKey:@"code"];
    if ([[Manager SQ_stringForKey:@"hexToken"] isNotEmpty]) {
        [dic setObject:[Manager SQ_stringForKey:@"hexToken"] forKey:@"deviceToken"];
    }
    WS(weakSelf);
    [HttpTool post:Url1 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[TXCommonHandler sharedInstance]cancelLoginVCAnimated:YES complete:^{
                UserModel * model = [UserModel mj_objectWithKeyValues:httpModel.data];
                [UserModel updateLocalUserModel:model];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Manager SQ_getViewController]];
                weakSelf.view.window.rootViewController = nav;
            }];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}


-(TXCustomModel*)SQ_getModel{
    TXCustomModel *model = [[TXCustomModel alloc] init];
        model.navColor = UIColor.blackColor;
        model.navTitle = [[NSAttributedString alloc] initWithString:@""attributes:@{NSForegroundColorAttributeName : UIColor.whiteColor,NSFontAttributeName : [UIFont systemFontOfSize:20.0]}];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *applocalversion = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    model.sloganText =[[NSAttributedString alloc] initWithString:applocalversion attributes:@{NSForegroundColorAttributeName : UIColor.whiteColor,NSFontAttributeName : FONT_SEMIBOLD(28)}];
    model.navBackImage = [UIImage imageNamed:@"返回键-01"];
    model.backgroundColor = UIColor.blackColor;
    model.prefersStatusBarHidden = YES;
    model.navColor = UIColor.clearColor;
        model.privacyNavColor = UIColor.blackColor;
        model.privacyNavBackImage = [UIImage imageNamed:@"icon_nav_back_light"];
        model.privacyNavTitleFont = [UIFont systemFontOfSize:20.0];
        model.privacyNavTitleColor = UIColor.whiteColor;
        model.logoImage = [UIImage imageNamed:@"logo"];
        model.checkBoxImages = @[SQGetImage(@"未选择"),SQGetImage(@"选择")];
        model.checkBoxIsChecked = YES;
        model.numberColor = UIColor.blackColor;
        model.numberFont = [UIFont systemFontOfSize:30.0];
        model.loginBtnText = [[NSAttributedString alloc] initWithString:SQNSLocalString(@"一键手机快捷登录") attributes:@{NSForegroundColorAttributeName : UIColor.whiteColor,NSFontAttributeName : FONT_MEDIUM(18)}];
       model.loginBtnBgImgs = @[SQGetImage(@"按钮背景"),SQGetImage(@"按钮背景"),SQGetImage(@"按钮背景")];
        model.privacyColors = @[SQColor999999,UIColor.whiteColor];
        model.privacyAlignment = NSTextAlignmentCenter;
        model.privacyFont = [UIFont fontWithName:@"PingFangSC-Regular" size:14.0];
        model.privacyOperatorPreText = @"《";
        model.privacyOperatorSufText = @"》";
        model.checkBoxWH = 17.0;
        model.changeBtnIsHidden = YES;
        model.preferredStatusBarStyle = UIStatusBarStyleLightContent;
        model.navMoreViewFrameBlock = ^CGRect(CGSize screenSize,CGSize superViewSize,CGRect frame) {
            CGFloat width = superViewSize.height;
            CGFloat height = width;
            return CGRectMake(superViewSize.width - 15 - width, 0, width, height);
        };
        model.loginBtnFrameBlock = ^CGRect(CGSize screenSize,CGSize superViewSize,CGRect frame) {
            CGRect frames = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 54);
             return frames;
        };
        model.sloganFrameBlock = ^CGRect(CGSize screenSize,CGSize superViewSize,CGRect frame) {
               return CGRectMake(0,140,superViewSize.width,frame.size.height);
        };
        model.numberFrameBlock = ^CGRect(CGSize screenSize,CGSize superViewSize,CGRect frame) {
           return frame;
        };
        
    model.supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
    return model;
}
 
@end
